﻿using EFCore.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SService;

namespace WMSWeb.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProductinformationController : ControllerBase
    {
        private readonly WmssystemContext myDb;
        private readonly ProductinformationS productinformationS;

        public ProductinformationController(WmssystemContext myDb, SService.ProductinformationS productinformationS)
        {
            this.myDb = myDb;
            this.productinformationS = productinformationS;
        }
        /// <summary>
        /// 货品信息查询全部
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<List<Pr_Productinformation>> Qu()
        {
            return await productinformationS.Qu();
        }
        /// <summary>
        /// 货品分页模糊查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("FenYe")]
        public async Task<List<Pr_Productinformation>> FenYe(int pageSize, int currentPage, string Key = null, string Status = null)
        {
            return await productinformationS.FenYe(pageSize, currentPage, Key, Status);
        }
        /// <summary>
        /// 货品添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<int>> Add(Pr_Productinformation proAdd)
        {
            decimal totalStockValue = proAdd.P_CurrentStock.Value * proAdd.P_OutboundPrice.Value;
            // 为实体的属性赋值
            proAdd.P_PIsDelete = true;
            proAdd.P_PDate = DateTime.Now;
            proAdd.P_PRen = "张三";
            proAdd.P_TotalStockValue = totalStockValue; // 设置计算得到的值  
            // 调用服务层的方法执行添加操作
            return await productinformationS.Add(proAdd);
        }
        /// <summary>
        /// 货品修改
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public async Task<ActionResult<int>> Update(Pr_Productinformation proUpd)
        {
            decimal totalStockValue = proUpd.P_CurrentStock.Value * proUpd.P_OutboundPrice.Value;
            proUpd.P_TotalStockValue = totalStockValue; // 设置计算得到的值  
            return await productinformationS.Upd(proUpd);
        }
        /// <summary>
        /// 货品逻辑删除
        /// </summary>
        /// <returns></returns>
        [HttpPut("UpdIsDelete")]
        public async Task<ActionResult<int>> UpdIsDelete(int id, bool isDeleted)
        {
            return await productinformationS.UpdIsDelete(id, isDeleted);
        }
    }
}
